package czk.concrent.synControl.practice2;

import java.util.concurrent.locks.LockSupport;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/10/28 10:46
 */
public class LockSupportSyn {
    private static Thread t1;
    private static Thread t2;
    private static Thread t3;

    public static void main(String[] args) {
        UnPark unPark = new UnPark(5);
        t1 = new Thread(() -> {
            unPark.print("a", t2);
        });
        t2 = new Thread(() -> {
            unPark.print("b", t3);
        });
        t3 = new Thread(() -> {
            unPark.print("c", t1);
        });
        // 启动
        t1.start();
        t2.start();
        t3.start();
        // 先解锁t1
        LockSupport.unpark(t1);
    }
}

class UnPark {
    int loopNum;

    public UnPark(int loopNum) {
        this.loopNum = loopNum;
    }

    public void print(String context, Thread t) {
        for (int i = 0; i < loopNum; i++) {
            // 阻塞
            LockSupport.park();
            // 打印
            System.out.print(context);
            // 解锁
            LockSupport.unpark(t);
        }
    }
}
